//
//  SHPictureViewCell.swift
//  SHWeibo
//
//  Created by jingming wang on 16/7/29.
//  Copyright © 2016年 cpy.name. All rights reserved.
//

import UIKit

class SHPictureViewCell: UICollectionViewCell {
    
    // MARK: - 懒加载控件
    // 背景图片
    private lazy var bgImageView: UIImageView = {
        let img = UIImageView(imgName: "weibo_placeholder")
        img.contentMode = .ScaleAspectFill
        img.layer.masksToBounds = true
        return img
    }()
    
    // 动态图片logo
    private lazy var gifImageView: UIImageView = UIImageView(imgName: "timeline_image_gif")
    
    // 数据模型
    var pic_url: SHPictureModel?{
        didSet{
            // 设置image
            bgImageView.sh_setImage(pic_url?.thumbnail_pic, placeholderImgName: "weibo_placeholder")
            // 判断是否为nil 而且后缀为gif （考虑到cell复用问题）
            if let thPic = pic_url?.thumbnail_pic where thPic.hasSuffix(".gif"){
                gifImageView.hidden = false
            }else {
                gifImageView.hidden = true
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 设置视图
    private func setupUI(){
        backgroundColor = UIColor.clearColor()
        // 01 添加控件
        contentView.addSubview(bgImageView)
        contentView.addSubview(gifImageView)
        // 02 添加约束
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        gifImageView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
    }

}

